package com.das.mechanic_base.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3CameraDetectionNewAdapter;
import com.das.mechanic_base.adapter.main.X3CarBothChoiceAdapter;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.alone.AloneCarBean;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.bean.camera.CameraBean;
import com.das.mechanic_base.bean.common.AwsFileBean;
import com.das.mechanic_base.bean.main.CarTyreAloneBean;
import com.das.mechanic_base.def.alone.AloneUploadStatus;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.mvp.view.camera.X3CameraPhotoActivity;
import com.das.mechanic_base.utils.X3AloneLayoutUtils;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class X3BottomHomeCarPaintDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3CarBothChoiceAdapter.IOnClickChoice, X3CameraDetectionNewAdapter.IOnItemCameraClick, X3CarPaintView.IOnClickChangeCar {

    private ImageView iv_cancel;
    private TextView tv_affirm;
    private ImageView iv_car;
    private X3MarqueeText tv_title;
    private LinearLayout ll_content;
    private NestedScrollView nv_view;
    private ImageView iv_pass;
    private X3CarPaintView cv_view;

    private Drawable pointDraw;

    private LinearLayout ll_title;

    private X3AloneLayoutUtils layoutUtils;

    /**
     * 根据sn 赋值拍照题目
     */
    private HashMap<String, AloneCarBean> cameraAllList;

    /**
     * 根据sn 查找选择题 用于全部通过功能
     */
    private HashMap<String, X3CarBothChoiceAdapter> choiceMap;

    /**
     * 用来记录滑动到哪一项
     */
    private List<View> viewList;

    /**
     * 存储答案
     */
    private HashMap<String,String> answerMap;

    private String cameraSn;
    private X3AlonePassDialog passDialog;
    private List<CarTyreAloneBean> mList;
    private long receiveBaseId;
    private PropertyValuesHolder valueOne,valueTwo;
    private ObjectAnimator carAnim;
    private int allNum;

    public X3BottomHomeCarPaintDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_affirm = getView(R.id.tv_affirm);
        iv_cancel = getView(R.id.iv_cancel);
        iv_car = getView(R.id.iv_car);
        ll_content = getView(R.id.ll_content);
        nv_view = getView(R.id.nv_view);
        tv_title = getView(R.id.tv_title);
        iv_pass = getView(R.id.iv_pass);
        cv_view = getView(R.id.cv_view);
        ll_title = getView(R.id.ll_title);

        cv_view.setiOnClickChangeCar(this);

        tv_affirm.setOnClickListener(this);
        iv_cancel.setOnClickListener(this);
        iv_pass.setOnClickListener(this);

        layoutUtils = new X3AloneLayoutUtils(mContext);

        pointDraw = mContext.getResources().getDrawable(R.mipmap.x3_point_pass);
        // 这一步必须要做,否则不会显示.
        pointDraw.setBounds(0, 0, pointDraw.getMinimumWidth(), pointDraw.getMinimumHeight());

        nv_view.setOnScrollChangeListener((View.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if (scrollY == 0) {
                cv_view.startAnim(114,1);
                iv_car.setImageResource(R.mipmap.x3_alone_car_front_point);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        EventBus.getDefault().register(this);
        return R.layout.x3_home_car_paint_bottom_dialog;
    }

    /**
     * 请求轮胎轮毂数据
     * @param questionToSystemSn
     */
    public void setQuestionSystemSn(String questionToSystemSn,long receiveBaseId) {
        this.receiveBaseId = receiveBaseId;
        NetWorkHttp.getApi().getQuestionSystemSn(questionToSystemSn, receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<CarTyreAloneBean>>() {
                    @Override
                    protected void onDone(List<CarTyreAloneBean> carTyreAloneBeans) {
                        if (X3StringUtils.isListEmpty(carTyreAloneBeans)) {
                            return;
                        }
                        showTyreView(carTyreAloneBeans);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    /**
     * 布局轮胎/轮毂的题
     * @param mList
     */
    private void showTyreView(List<CarTyreAloneBean> mList) {
        this.mList = mList;
        ll_content.removeAllViews();
        choiceMap = new HashMap<>();
        cameraAllList = new HashMap<>();
        answerMap = new HashMap<>();
        viewList = new ArrayList<>();
        allNum = 0;
        for (int i = 0; i < mList.size(); i++) {
            CarTyreAloneBean tyreAloneBean = mList.get(i);
            String sn = tyreAloneBean.sn;
            List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList = tyreAloneBean.questionBaseEntityList;

            if (i > 0) {
                View lineView = new View(mContext);
                LinearLayout.LayoutParams lineLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        X3ScreenUtils.dipToPx(10,mContext));
                lineView.setLayoutParams(lineLayout);
                lineView.setBackgroundColor(Color.parseColor("#F5F5F7"));
                ll_content.addView(lineView);
            }

            View titleView = View.inflate(mContext,R.layout.x3_detection_title_item,null);
            TextView tv_title = titleView.findViewById(R.id.tv_title);
            TextView tv_set = titleView.findViewById(R.id.tv_set);
            tv_set.setVisibility(View.INVISIBLE);

            ll_content.addView(titleView);

            tv_title.setText(tyreAloneBean.toItem + "");

            X3MyRecyclerview photoRecy = layoutUtils.getDetectionRecyclerview();
            List<AloneImageAndRecordBean> imageList = new ArrayList<>();
            List<CarTyreAloneBean.MimeTupleListBean> mimeTupleList = tyreAloneBean.mimeTupleList;
            if (!X3StringUtils.isListEmpty(mimeTupleList)) {
                for (CarTyreAloneBean.MimeTupleListBean mimeTupleListBean : mimeTupleList) {
                    AloneImageAndRecordBean imageAndRecordBean = new AloneImageAndRecordBean(mimeTupleListBean.value,mimeTupleListBean.value,mimeTupleListBean.key,AloneUploadStatus.UPLOADING_SUCCESS);
                    imageList.add(imageAndRecordBean);
                }
            }

            X3CameraDetectionNewAdapter detectionNewAdapter = new X3CameraDetectionNewAdapter(mContext, true,sn);
            photoRecy.setAdapter(detectionNewAdapter);
            detectionNewAdapter.changeData(imageList);
            detectionNewAdapter.setiOnItemCameraClick(this);
            cameraAllList.put(sn,new AloneCarBean(photoRecy,detectionNewAdapter));
            ll_content.addView(photoRecy);

            if (X3StringUtils.isListEmpty(questionList)) {
                continue;
            }
            allNum += questionList.size();

            showQuestionItem(questionList,tyreAloneBean.toItem,titleView);
        }
    }

    /**
     * 显示题目
     * @param questionList
     */
    private void showQuestionItem(List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList,String title,View titleView) {
        for (int i = 0; i < questionList.size(); i++) {
            CarTyreAloneBean.QuestionBaseEntityListBean questionBean = questionList.get(i);
            if (questionBean == null) {
                return;
            }
            String type = questionBean.type;
            String sn = questionBean.sn;
            String description = questionBean.description;
            String answer = questionBean.answer;

            answerMap.put(sn,answer);

            if ("sys_paint_0001_01".equals(sn) || "sys_paint_0003_01".equals(sn) || "sys_paint_0007_01".equals(sn)
                    || "sys_paint_0009_01".equals(sn) || "sys_paint_0013_01".equals(sn)) {
                viewList.add(titleView);
            }

            View itemView = null;
            if ("CHOICE".equals(type)) {
                itemView = View.inflate(mContext,R.layout.x3_detection_choice_item,null);
                LinearLayout.LayoutParams choiceLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                choiceLayout.leftMargin = X3ScreenUtils.dipToPx(16,mContext);
                choiceLayout.rightMargin = X3ScreenUtils.dipToPx(16,mContext);
                choiceLayout.bottomMargin = X3ScreenUtils.dipToPx(28,mContext);
                itemView.setLayoutParams(choiceLayout);
                TextView tv_title = itemView.findViewById(R.id.tv_title);
                RecyclerView rlv_choice = itemView.findViewById(R.id.rlv_choice);

                tv_title.setText(questionBean.description + "");
                rlv_choice.setLayoutManager(new LinearLayoutManager(mContext));
                List<String> optionsList = questionBean.optionsList;
                X3CarBothChoiceAdapter bothChoiceAdapter = new X3CarBothChoiceAdapter(optionsList,mContext,sn);
                rlv_choice.setAdapter(bothChoiceAdapter);
                bothChoiceAdapter.setiOnClickChoice(this);
                bothChoiceAdapter.changeSelectChoice(answer);

                choiceMap.put(sn,bothChoiceAdapter);

                ll_content.addView(itemView);
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_cancel) {
            dismiss();
        } else if (id == R.id.tv_affirm) {/**
         * TODO 保存车漆初检信息
         */
            if (!X3Utils.isFastClick()) {
                return;
            }
            if (X3StringUtils.isListEmpty(mList)) {
                return;
            }
            for (int i = 0; i < mList.size(); i++) {
                CarTyreAloneBean tyreAloneBean = mList.get(i);
                List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList = tyreAloneBean.questionBaseEntityList;
                List<CarTyreAloneBean.QuestionBaseEntityListBean> newQuestionList = new ArrayList<>();

                String tyreSn = tyreAloneBean.sn;
                AloneCarBean carBean = cameraAllList.get(tyreSn);
                List<Long> idList = new ArrayList<>();
                if (carBean != null) {
                    List<AloneImageAndRecordBean> cameraList = carBean.getDetectionNewAdapter().getCameraList();
                    if (!X3StringUtils.isListEmpty(cameraList)) {
                        for (AloneImageAndRecordBean imageBean : cameraList) {
                            if (imageBean.getId() == 0) {
                                continue;
                            }
                            idList.add(imageBean.getId());
                        }
                    }
                }

                tyreAloneBean.resourceIdList = idList;

                if (X3StringUtils.isListEmpty(questionList)) {
                    mList.remove(i);
                    mList.add(i, tyreAloneBean);
                    continue;
                }
                for (int j = 0; j < questionList.size(); j++) {
                    CarTyreAloneBean.QuestionBaseEntityListBean questionBean = questionList.get(j);
                    String sn = questionBean.sn;
                    String content = answerMap.get(sn);
                    if (X3StringUtils.isEmpty(content)) {
                        questionBean.answer = "";
                    } else {
                        String afterAnswer = content.replace("A. ", "").replace("B. ", "").replace("C. ", "");
                        questionBean.answer = afterAnswer;
                    }
                    newQuestionList.add(questionBean);
                }
                tyreAloneBean.questionBaseEntityList = newQuestionList;
                mList.remove(i);
                mList.add(i, tyreAloneBean);
            }

            /**
             * 保存或者更新初检轮胎信息
             */
            saveDetection(mList);
        } else if (id == R.id.iv_pass) {/**
         * 全部通过
         */
            if (X3StringUtils.isListEmpty(mList)) {
                return;
            }
            int allNum = 0;
            for (int i = 0; i < mList.size(); i++) {
                List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList = mList.get(i).questionBaseEntityList;
                if (X3StringUtils.isListEmpty(questionList)) {
                    continue;
                }
                for (CarTyreAloneBean.QuestionBaseEntityListBean questionBean : questionList) {
                    String type = questionBean.type;
                    if (!"CHOICE".equals(type)) {
                        continue;
                    }
                    allNum++;
                }
            }
            passDialog = new X3AlonePassDialog((Activity) mContext);
            passDialog.show();
            passDialog.changePassDialog(mContext.getString(R.string.x3_all_pass),
                    String.format(allNum == 1 ? mContext.getString(R.string.x3_pass_num_answer) : mContext.getString(R.string.x3_pass_num_answers), allNum), false);
            passDialog.setiBtnClick(() -> {
                for (int i = 0; i < mList.size(); i++) {
                    List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList = mList.get(i).questionBaseEntityList;
                    if (X3StringUtils.isListEmpty(questionList)) {
                        continue;
                    }
                    for (CarTyreAloneBean.QuestionBaseEntityListBean questionBean : questionList) {
                        String type = questionBean.type;
                        if (!"CHOICE".equals(type)) {
                            continue;
                        }
                        X3CarBothChoiceAdapter bothChoiceAdapter = choiceMap.get(questionBean.sn);
                        if (bothChoiceAdapter != null) {
                            String answer = choiceMap.get(questionBean.sn).changeAllPass();
                            answerMap.put(questionBean.sn, answer);
                        }
                    }
                }
            });
        }
    }

    /**
     * 保存或者更新初检轮胎信息
     * @param mList
     */
    private void saveDetection(List<CarTyreAloneBean> mList) {
        NetWorkHttp.getApi().saveDetectionFirestInfo(mList,receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        dismiss();
                        if (iOnRefresh != null) {
                            iOnRefresh.iOnRefreshData();
                        }
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void iOnClickChoiceContent(String content, String sn) {
        /**
         * TODO 选择题
         */
        answerMap.put(sn,content);
    }

    @Override
    public void iOnItemCameraClick(String sn) {
        XXPermissions.with((Activity) mContext)
                .permission(Permission.CAMERA)
                .permission(Permission.RECORD_AUDIO)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if(all){
                            cameraSn = sn;
                            // 当前已经拍摄的照片或者视频的数量
                            int size = cameraAllList.get(cameraSn).getDetectionNewAdapter().getCameraList().size();
                            if (9 - size == 0) {
//                                ToastUtils.showMessage(mContext.getString(R.string.upload_mast_nine));
                                X3ToastUtils.showMessage(String.format(mContext.getString(R.string.x3_upload_mast_nine),9));
                                return;
                            }
                            // TODO 跳转到拍照页面
                            //changeRlCamera(View.VISIBLE);

                            Intent intent = new Intent(mContext, X3CameraPhotoActivity.class);
                            intent.putExtra("allNum",9 - size);
                            intent.putExtra("isAlbum",2);
                            mContext.startActivity(intent);
                            ((Activity)mContext).overridePendingTransition(R.anim.activity_enter, 0);
                        }else {
                            XXPermissions.startPermissionActivity(mContext, permissions);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        XXPermissions.startPermissionActivity(mContext, permissions);
                    }
                });
    }

    @Override
    public void iOnDeleteClick(long resId, int position, String sn, String path) {
        saveOrUpdateWorkResource(resId,sn,false,path);
    }

    @Override
    public void iOnAgainUploadImage(String path, String sn, int imageIndex) {
        uploadImageAws(new File(path),sn,false);
    }

    /**
     * 从相册中选取图片 或者拍照 数据都会从这个方法进入
     * @param cameraBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetPictureOrVideo(CameraBean cameraBean) {
        if (cameraBean == null) {
            return;
        }
        switch ((int) cameraBean.resultCode) {
            //图片或者视频
            case X3Constent.CODE_CAMERA_REQUEST:
            case X3Constent.CODE_GALLERY_REQUEST:
                if (cameraBean.cameraData == null) {
                    return;
                }
                List<String> pList = (List<String>) cameraBean.cameraData;

                for (String path : pList) {
                    if (!X3StringUtils.isEmpty(path)) {
                        cameraAllList.get(cameraSn).getDetectionNewAdapter().addData(new AloneImageAndRecordBean(path,"",0,AloneUploadStatus.UPLODING));

                        uploadImageAws(new File(path),cameraSn,false);
                    }
                }
                break;
            default:
                break;
        }
    }

    private void uploadImageAws(File imgFile,String sn,boolean isSound) {
        if (!imgFile.exists()) {
            return;
        }

        Map<String,Object> imageMap = new HashMap<>();
        imageMap.put("createTime", X3FileUtils.getFileLastModifiedTime(imgFile));
        imageMap.put("platform", Build.BRAND + " " + Build.MODEL);

        RequestBody requestBody;
        if (imgFile.getPath().endsWith(".mp4") || imgFile.getPath().endsWith(".mov")) {
            requestBody = RequestBody.create(MediaType.parse("video/mp4"), imgFile);
            MediaMetadataRetriever retr = new MediaMetadataRetriever();
            retr.setDataSource(imgFile.getPath());
            String height = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT); // 视频高度
            String width = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);

            imageMap.put("width",width + "");
            imageMap.put("height",height + "");

        }else {
            requestBody = RequestBody.create(MediaType.parse("image/jpg"), imgFile);

            Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getPath());
            imageMap.put("width",bitmap.getWidth() + "");
            imageMap.put("height",bitmap.getHeight() + "");

            if (bitmap != null) {
                bitmap.recycle();
            }
        }
        try {
            String  filename = URLEncoder.encode(imgFile.getName(), "UTF-8");
            MultipartBody.Part fileBody = MultipartBody.Part.createFormData("multipartFile", filename, requestBody);

            Map<String,RequestBody> map = new HashMap<>();

            map.put("description",RequestBody.create(MediaType.parse("multipart/form-data"), "检测图片"));

            map.put("ossBucketName",RequestBody.create(MediaType.parse("multipart/form-data"), "SERVICE_PIC"));

            NetWorkHttp.getApi().uploadFileAws(map,fileBody,imageMap)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(((X3IBaseView)mContext).bindToLife())
                    .subscribe(new HttpCallBack<AwsFileBean>() {
                        @Override
                        protected void onDone(AwsFileBean awsFileBean) {
                            saveOrUpdateWorkResource(awsFileBean.id,sn,true,imgFile.getPath());
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {
                            saveOrUpdateWorkResource(0,sn,true,imgFile.getPath());
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 更新精检图片
     * @param resId
     * @param sn
     */
    public void saveOrUpdateWorkResource(long resId,String sn,boolean isUpload,String path) {
        /**
         * 如果resId == 0 说明上传失败直接回调
         */
        getUpdatePictureSuccess(new AloneImageAndRecordBean(path,path,resId, resId == 0 ? AloneUploadStatus.UPLOADING_FAILED : AloneUploadStatus.UPLOADING_SUCCESS),sn,isUpload);
    }

    /**
     * 更新图片成功
     * @param aloneImageAndRecordBean
     * @param sn
     * isUpload 是否是上传图片  false为删除图片
     */
    public void getUpdatePictureSuccess(AloneImageAndRecordBean aloneImageAndRecordBean, String sn,boolean isUpload) {
        if (aloneImageAndRecordBean == null) {
            return;
        }
        List<AloneImageAndRecordBean> cameraList = cameraAllList.get(sn).getDetectionNewAdapter().getImgList();
        if (X3StringUtils.isListEmpty(cameraList)) {
            return;
        }
        int refreshIndex = -1;
        String currentPath = aloneImageAndRecordBean.getPath();
        for (int i = 0; i < cameraList.size(); i++) {
            String path = cameraList.get(i).getPath();

            long uploadStatus = cameraList.get(i).getUploadStatus();
            if (X3StringUtils.isEmpty(path)) {
                continue;
            }
            if (isUpload) {
                if (!X3StringUtils.isEmpty(currentPath) && currentPath.equals(path)
                        && (uploadStatus == AloneUploadStatus.UPLODING || uploadStatus == AloneUploadStatus.UPLOADING_FAILED)) {
                    refreshIndex = i;
                    break;
                }
            }else {
                if (!X3StringUtils.isEmpty(currentPath) && currentPath.equals(path)) {
                    refreshIndex = i;
                    break;
                }
            }
        }
        if (refreshIndex == -1) {
            return;
        }

        long uploadStatus = aloneImageAndRecordBean.getUploadStatus();
        int realStatus = ((uploadStatus == AloneUploadStatus.UPLODING || uploadStatus == AloneUploadStatus.UPLOADING_SUCCESS) ? AloneUploadStatus.UPLOADING_SUCCESS : AloneUploadStatus.UPLOADING_FAILED);
        aloneImageAndRecordBean.setUploadStatus(realStatus);
        if (isUpload) {
            cameraAllList.get(sn).getDetectionNewAdapter().changeData(aloneImageAndRecordBean,refreshIndex);

        }else {
            cameraAllList.get(sn).getDetectionNewAdapter().deleteImage(refreshIndex);
        }

    }


    @Override
    public void dismiss() {
        EventBus.getDefault().unregister(this);
        if (iv_car != null && iv_car.getAnimation() != null) {
            iv_car.getAnimation().cancel();
        }
        if (carAnim != null) {
            carAnim.cancel();
        }
        super.dismiss();
    }

    IOnRefresh iOnRefresh;

    public void setiOnRefresh(IOnRefresh iOnRefresh) {
        this.iOnRefresh = iOnRefresh;
    }

    @Override
    public void iOnClickChange(int type) {
        iv_car.animate().alpha(0).setDuration(200).start();
        switch (type) {
            case 0:
                /**
                 * 背面
                 */
                valueOne = PropertyValuesHolder.ofFloat("alpha", 0f,1f);
                carAnim = ObjectAnimator.ofPropertyValuesHolder(iv_car, valueOne);
                carAnim.setStartDelay(200);
                carAnim.setDuration(500);
                carAnim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        if (!isShowing()) {
                            return;
                        }
                        iv_car.setImageResource(R.mipmap.x3_alone_car_back_point);
                    }
                });

                ll_title.post(() -> {
                    if (!X3StringUtils.isListEmpty(viewList) && viewList.size() > 2) {
                        nv_view.scrollTo(0,viewList.get(2).getTop() + ll_title.getHeight());
                    }
                });


                break;
            case 1:
                /**
                 * 正面
                 */
                valueOne = PropertyValuesHolder.ofFloat("alpha", 0f,1f);
                carAnim = ObjectAnimator.ofPropertyValuesHolder(iv_car, valueOne);
                carAnim.setStartDelay(200);
                carAnim.setDuration(500);
                carAnim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        if (!isShowing()) {
                            return;
                        }
                        iv_car.setImageResource(R.mipmap.x3_alone_car_front_point);
                    }
                });

                ll_title.post(() -> {
                    if (!X3StringUtils.isListEmpty(viewList)) {
                        nv_view.scrollTo(0,viewList.get(0).getTop() + ll_title.getHeight());
                    }
                });


                break;
            case 2:
                /**
                 * 左面
                 */
                valueOne = PropertyValuesHolder.ofFloat("alpha", 0f,1f);
                valueTwo = PropertyValuesHolder.ofFloat("translationX", X3ScreenUtils.getScreenWidth(mContext),0);
                carAnim = ObjectAnimator.ofPropertyValuesHolder(iv_car, valueOne,valueTwo);
                carAnim.setStartDelay(200);
                carAnim.setDuration(500);
                carAnim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        if (!isShowing()) {
                            return;
                        }
                        iv_car.setImageResource(R.mipmap.x3_alone_car_left_point);
                    }
                });

                ll_title.post(() -> {
                    if (!X3StringUtils.isListEmpty(viewList) && viewList.size() > 1) {
                        nv_view.scrollTo(0,viewList.get(1).getTop() + ll_title.getHeight());
                    }
                });


                break;
            case 3:
                /**
                 * 右面
                 */
                valueOne = PropertyValuesHolder.ofFloat("alpha", 0f,1f);
                valueTwo = PropertyValuesHolder.ofFloat("translationX", -X3ScreenUtils.getScreenWidth(mContext),0);
                carAnim = ObjectAnimator.ofPropertyValuesHolder(iv_car, valueOne,valueTwo);
                carAnim.setStartDelay(200);
                carAnim.setDuration(500);
                carAnim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        if (!isShowing()) {
                            return;
                        }
                        iv_car.setImageResource(R.mipmap.x3_alone_car_right_point);
                    }
                });

                ll_title.post(() -> {
                    if (!X3StringUtils.isListEmpty(viewList) && viewList.size() > 3) {
                        nv_view.scrollTo(0,viewList.get(3).getTop() + ll_title.getHeight());
                    }

                });

                break;
            case 4:
                /**
                 * 上面
                 */
                valueOne = PropertyValuesHolder.ofFloat("alpha", 0f,1f);
                valueTwo = PropertyValuesHolder.ofFloat("translationY", X3ScreenUtils.dipToPx(200,mContext),0);
                carAnim = ObjectAnimator.ofPropertyValuesHolder(iv_car, valueOne,valueTwo);
                carAnim.setStartDelay(200);
                carAnim.setDuration(500);
                carAnim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        if (!isShowing()) {
                            return;
                        }
                        iv_car.setImageResource(R.mipmap.x3_alone_car_top_point);
                    }
                });

                ll_title.post(() -> {
                    if (!X3StringUtils.isListEmpty(viewList) && viewList.size() > 4) {
                        nv_view.scrollTo(0,viewList.get(4).getTop() + ll_title.getHeight());
                    }
                });


                break;
        }
        carAnim.start();
    }



    /**
     * 刷新数据
     */
    public interface IOnRefresh {
        void iOnRefreshData();
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息车漆初检弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息车漆初检弹窗");
    }
}
